package com.yanqu.road.server.cmd.activity.relation.cross;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.cookboyactivity.CookBoyActivityProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.RelationActivityModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.DecorationModule;
import com.yanqu.road.server.logic.activity.relation.RelationActivityData;
import com.yanqu.road.server.manger.activity.RelationActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.Map;


@Cmd(code = Protocol.S_CROSS_COOK_BOY_GET_RANK_REWARD_NOTIFY, desc = "跨服领取排行奖励通知")
public class CrossRelationGetRankNotifyCmd extends AbstractCommand {
    private int activityId;
    private int conditionId;
    private int myRank;
    private ActivityRewardResult rewardResult;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossActivityProto.CrossActivityGetRewardRespMsg reqMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        myRank = reqMsg.getRank();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RelationActivityModule activityModule = player.getModule(RelationActivityModule.class);
        CrossActivityProto.CrossActivityGetRewardRespMsg reqMsg = CrossActivityProto.CrossActivityGetRewardRespMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionId = reqMsg.getConditionId();
        myRank = reqMsg.getRank();
        int position = reqMsg.getPosition();
        rewardResult = new ActivityRewardResult();//player.getModule(RelationActivityModule.class).getCrossRankReward(activityId, conditionId,  myRank);
        //	ActivityRewardResult result = new ActivityRewardResult();
        ActivityInfo activityInfo = RelationActivityMgr.getOpenActivityInfo(activityId);
        ActivityConditionInfo activityConditionInfo = RelationActivityMgr.getActivityConditionInfo(activityId, conditionId);
        Map<Integer, RelationActivityData> dataMap = activityModule.getActivityDataMap().get(activityId);
        RelationActivityData activityData = dataMap.get(activityConditionInfo.getType());
        UserActivityConditionData conditionData = activityData.getFirstConditionData();
        ActivityConditionInfo conditionInfo = RelationActivityMgr.getActivityConditionInfoByRank(activityId, activityConditionInfo.getType(), myRank);
        if (null == conditionInfo) {
            rewardResult.setRet(GameErrorCode.E_ACTIVITY_CONDITION_NO_COMPLETE);
            return;
        }
        if (null == conditionData || conditionData.isGetReward()) {
            rewardResult.setRet(GameErrorCode.E_ACTIVITY_REWARD_IS_GET);
            return;
        }
        if (RelationActivityMgr.getRankEvent(activityId, RelationActivityMgr.EVENT_RANK_UNION_TYPE) == conditionInfo.getType()) {
            rewardResult.setReward(UnionActivityMgr.getUnionMemberPositionDataReward(position, conditionInfo, player.getUserId()));
            player.getModule(CurrencyModule.class).addCurrency(rewardResult.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityUnionRankReward);
        } else {
            player.getModule(CurrencyModule.class).addCurrency(conditionInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.CrossCookBoyActivityRankReward);
            rewardResult.setReward(conditionInfo.getRewardStr());
        }
        conditionData.setGetReward(true);
        activityModule.onActivityDataChange(activityData);
        activityData.finish(conditionData.getConditionId());

        String rewardStr = rewardResult.getReward();
        if (rewardStr != null) {
            Property property = PropertyHelper.parseStringToProperty(rewardStr);
            if (property != null) {
                //记录跨服称号包含的区服id
                property.getGoods().forEach((k, v) -> {
                    if (GoodsMgr.isDecorationGoods(k)) {
                        player.getModule(DecorationModule.class).setServerIds(k, activityInfo.getActivityId(), activityInfo.getServerListStr());
                    }
                });
                player.getModule(BanquetModule.class).getCrossBanquetInvitationReward(rewardStr, activityInfo.getServerListStr());
            }
        }

        LogMgr.addLogActivityReward(player.getUserId(), activityId, activityInfo.getType(), activityInfo.getChildType(),
                activityConditionInfo.getConditionId(), myRank, false, rewardResult.getReward());
    }

    @Override
    public void sendData(GamePlayer player) {
        CookBoyActivityProto.CookBoyActivityGetRewardRespMsg.Builder respMsg = CookBoyActivityProto.CookBoyActivityGetRewardRespMsg.newBuilder();
        respMsg.setRet(rewardResult.getRet());
        if (0 == respMsg.getRet()) {
            respMsg.setReward(rewardResult.getReward());
        }
        player.sendPacket(Protocol.U_COOK_BOY_ACTIVITY_GET_REWARD, respMsg);
    }

}
